package com.example.weatherreport.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.icu.util.TimeUnit;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.example.weatherreport.bean.Weather;
import com.example.weatherreport.net.callback.WeatherCallback;
import com.example.weatherreport.net.WeatherHelper;
import com.example.weatherreport.util.AppUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 空气质量后台定时通知Service
 */
public class WeatherQualityService extends Service {
    private static final String TAG = "WeatherQualityService";

    private Timer mTimer;

    private int mNotificationId = 0;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");

        createNotification();
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                //先判断程序是否在后台
                if (AppUtil.isBackground(WeatherQualityService.this)) {
                    WeatherHelper.queryWeather(new WeatherCallback() {
                        @Override
                        public void onResult(Weather weather) {
                            Log.i(TAG, "onResult: " + weather.getAir_tips());
                            showAirNotification(weather.getAir(), weather.getAir_tips());
                        }
                    });
                }
            }
        }, 1000, 1000);//延迟一秒，1秒为一个周期,根据具体需求进行设置
    }

    private void createNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "air";
            String channelName = "空气质量";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);

            channelId = "city";
            channelName = "城市";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(channelId, channelName, importance);
        }
    }

    /**
     * 展示空气质量的通知
     *
     * @param air
     * @param tip
     */
    private void showAirNotification(String air, String tip) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "air")
                .setContentTitle("空气质量：" + air)
                .setContentText(tip)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(android.R.drawable.ic_delete)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_agenda))
                .setAutoCancel(true)
                .build();
        manager.notify(mNotificationId++, notification);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        super.onDestroy();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }
}
